package hyl.core;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class MyCharset {
	public static final String ISO8859 = "ISO-8859-1";
	public static final String GB2312 = "GB2312";
	public static final String GBK = "GBK";
	public static final String GB18030 = "GB18030";
	public static final String UTF8 = "UTF-8";
	public static final String UTF16 = "UTF-16";
	public static final String UNICODE = "UNICODE";
	public static final String UTF16L = "UTF-16LE";
	public static final String UTF16B = "UTF-16BE";
	public static final Charset _UTF8 = Charset.forName(UTF8);
	public static final Charset _GB2312 = Charset.forName(GB2312);
	public static final Charset _ISO8859 = Charset.forName(ISO8859);
	public static final Charset _UNICODE = Charset.forName(UNICODE);
	public static final Charset _GBK = Charset.forName(GBK);

	public static String GBKToISO8859(String s) throws Exception {
		return new String(s.getBytes("GBK"), "ISO8859_1");
	}

	public static String Unicode2Utf8(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(), UTF8);
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}

	public static String UTF8ToUnicode(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(UTF8), UNICODE);
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}

	public static String UTF8ToUTF16(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(UTF8), UTF16);
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}

	public static String UTF8ToISO8859(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(UTF8), ISO8859);
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}

	public static String ISO8859ToUTF8(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(ISO8859), UTF8);
		} catch (Exception e) {
			return s;
		}
	}

	public static String ISO8859ToUnicode(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(ISO8859), UNICODE);
		} catch (Exception e) {
			return s;
		}
	}

	public static String ISO8859ToUTF16(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(ISO8859), UTF16);
		} catch (Exception e) {
			return s;
		}
	}

	public static String ISO8859ToGB2312(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(ISO8859), GB2312);
		} catch (Exception e) {
			return s;
		}
	}

	public static String ISO8859ToGBK(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(ISO8859), "GBK");
		} catch (Exception e) {
			return s;
		}
	}

	public static String ISO8859ToGB18030(String s) {
		try {
			if (MyFun.isEmpty(s)) {
				return "";
			}
			return new String(s.getBytes(ISO8859), "GB18030");
		} catch (Exception e) {
			return s;
		}
	}
}
